﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace eOrdered.Helpers
{
    public class CustomViewEngine : IViewEngine
    {
        //Properties
        public IViewEngine BaseViewEngine { get; private set; }
        public Func<ControllerContext, bool> IsCorrectDevice { get; private set; }
        public string PathToSearch { get; private set; }

        // Constructor
        public CustomViewEngine(IViewEngine baseViewEngine, Func<ControllerContext, bool> isCorrectDevice, string pathToSearch)
        {
            BaseViewEngine = baseViewEngine;
            IsCorrectDevice = isCorrectDevice;
            PathToSearch = pathToSearch;
        }

    
#region IViewEngine Members

        public ViewEngineResult  FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
 	        if (IsCorrectDevice(controllerContext))
 	        {
 	            return BaseViewEngine.FindPartialView(controllerContext, PathToSearch + "/" + partialViewName, useCache);
 	        }
            return new ViewEngineResult(new string[] { });
        }

        public ViewEngineResult  FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
 	        if (IsCorrectDevice(controllerContext))
 	        {
 	            return BaseViewEngine.FindView(controllerContext, PathToSearch + "/" + viewName, masterName, useCache);
 	        }

            return new ViewEngineResult(new string[] { });
        }

        public void  ReleaseView(ControllerContext controllerContext, IView view)
        {
 	        throw new NotImplementedException();
        }

#endregion
}
}